<template>
  <el-dialog
    title="修改关系"
    :visible.sync="visible"
    :before-close="handleClose"
    width="40%"
  >
    <div>
      <el-form ref="form" :model="form">
        <el-form-item v-if="val.nick_name" label="是否确认将员工：">
          {{ val.nick_name }}下的客户转移至
        </el-form-item>
        <el-form-item v-else>
          请指定选择以下用户为分销员：
        </el-form-item>
        <el-form-item label="选择归属门店">
          <el-select
            v-model="form.store_id"
            placeholder="请选择"
            size="small"
            style="margin-right:10px;"
            @change="getList"
          >
            <el-option
              v-for="item in options"
              :key="item.store_id"
              :label="item.name"
              :value="item.store_id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <Table :data="listData" :max-height="360">
          <el-table-column align="center" width="55">
            <template slot-scope="scope">
              <el-radio
                v-model="scope.row.radio"
                :label="1"
                @change="radioTag(scope.row.member_id)"
              >
                <span style="display:none">dsd</span>
              </el-radio>
            </template>
          </el-table-column>
          <el-table-column
            label="员工名称"
            align="center"
            property="nick_name"
          ></el-table-column>
          <el-table-column label=" 员工头像" align="center">
            <template slot-scope="scope">
              <img
                :src="
                  $store.state.qiniu + scope.row.cover + $store.state.imgRotate
                "
                width="55px"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="所属门店"
            align="center"
            property="phone"
          ></el-table-column>
        </Table>
        <el-pagination
          style="text-align: center;margin-top: 10px;"
          :page-size="4"
          layout="prev, pager, next"
          :total="totals"
          @current-change="changes"
        >
        </el-pagination>
      </el-form>
    </div>
    <span slot="footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSumbit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { STORE_LIST } from "@/service/shop";
import { EDITSTAFFCHAIN, STAFFCHAIN } from "../../../service";

export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    val: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      listData: [],
      Arr: [],
      pages: 1,
      totals: 0,
      form: {
        store_id: ""
      },
      options: []
    };
  },
  watch: {
    visible(v) {
      if (v) {
        this.getList();
        this.store();
      }
    }
  },
  methods: {
    handleSelectionChange(e) {
      this.Arr = e;
    },
    handleClose() {
      this.$emit("outDialog");
    },
    async handleSumbit() {
      let json = this.listData.filter(ele => {
        return ele.radio === 1;
      });
      await EDITSTAFFCHAIN({
        type: this.form.type,
        member_ids: this.val.member_id,
        distributor: json[0].member_id
      }).then(({ code, msg }) => {
        if (code === 200) {
          this.$emit("outDialog", true);
        }
        this.$message({
          message: msg,
          type: code === 200 ? "success" : "error"
        });
      });
    },
    async getList() {
      let param = {
        page: this.pages,
        pageshow: 4,
        member_ids: this.val.member_id,
        store_id: this.form.store_id
      };
      await STAFFCHAIN(param).then(({ code, data, msg }) => {
        if (code === 200) {
          this.totals = data.count;
          data.list.forEach(ele => {
            ele.radio = 2;
          });
          this.listData = data.list;
        } else {
          this.totals = 0;
          this.listData = [];
          this.$message.error(msg);
        }
      });
    },
    changes(page) {
      this.pages = page;
      this.getList();
    },
    radioTags() {
      this.page = 1;
      this.getList();
    },
    radioTag(id) {
      this.listData.forEach(ele => {
        if (ele.member_id === id) {
          ele.radio = 1;
        } else {
          ele.radio = 2;
        }
      });
    },
    async store() {
      let param = {
        page: 1,
        pageshow: 100000
      };
      await STORE_LIST(param).then(({ code, data }) => {
        if (code === 200) {
          data.data.unshift({ store_id: 0, name: "全部门店" });
          this.options = data.data;
        } else {
          this.options = [];
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.divf {
  float: left;
}
.name {
  margin-left: 10px;
}
</style>
